package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pms.entity.Users;
import com.pms.util.DBHelp;
import com.pms.util.RowMapper;

public class UserDao {
	
	/**
	 * author: handy
	 * 2013-03-11
	 * 
	 */
	DBHelp<Users> db = new DBHelp<Users>();
	
	public Users findByNameAndPassword(String name, String pwd){
		String sql = "SELECT useid, username, `password`, pic FROM t_user WHERE username = ? AND `password` = ? ";
		return db.executeQueryToObject(sql, new UserRowMapper(), name, pwd);
	}
	
	public Users findById(String id){
		String sql = "SELECT useid, username, `password`, pic FROM t_user WHERE useid = ? ";
		return db.executeQueryToObject(sql, new UserRowMapper(), id);
	}
	
	public List<Users> findAllUsers() {
		String sql = "SELECT useid, username, `password`, pic FROM t_user";
		return db.executeQueryToList(sql, new UserRowMapper());
	}
	
	private class UserRowMapper implements RowMapper<Users>{

		@Override
		public Users mapRow(ResultSet rs) throws SQLException {
			Users users = new Users();
			users.setUser_id(rs.getString("useid"));
			users.setUser_name(rs.getString("username"));
			users.setUser_password(rs.getString("password"));
			users.setUser_pic(rs.getString("pic"));
			return users;
		}
		
	}

}
